//给你一个二叉树，请你返回其按 层序遍历 得到的节点值。 （即逐层地，从左到右访问所有节点）。
//
//
//
// 示例：
//二叉树：[3,9,20,null,null,15,7],
//
//
//    3
//   / \
//  9  20
//    /  \
//   15   7
//
//
// 返回其层序遍历结果：
//
//
//[
//  [3],
//  [9,20],
//  [15,7]
//]
//
// Related Topics 树 广度优先搜索 二叉树
// 👍 1130 👎 0

package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * [102]二叉树的层序遍历
 */
public class BinaryTreeLevelOrderTraversal {
    public static void main(String[] args) {
        Solution solution = new BinaryTreeLevelOrderTraversal().new Solution();
        TreeNode head = new TreeNode(3);
        TreeNode headLeft = new TreeNode(9);
        TreeNode headRight = new TreeNode(20);
        head.left = headLeft;
        head.right = headRight;
        TreeNode headRightLeft = new TreeNode(15);
        TreeNode headRightRight = new TreeNode(7);
        headRight.left = headRightLeft;
        headRight.right = headRightRight;
        List<List<Integer>> result = solution.levelOrder(head);
        System.out.println(result);
    }
    //leetcode submit region begin(Prohibit modification and deletion)
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */

    static public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        // 递归遍历
        // 略

        // 网上最优解
        public List<List<Integer>> levelOrder(TreeNode root) {
            if (root == null) {
                return new ArrayList<>();
            }
            List<List<Integer>> result = new ArrayList<>();
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(root);
            while (!queue.isEmpty()) {
                List<Integer> line = new ArrayList<>();
                int count = queue.size();
                for (int i = 0; i < count; i++) {
                    TreeNode poll = queue.poll();
                    line.add(poll.val);
                    if (poll.left != null) {
                        queue.offer(poll.left);
                    }
                    if (poll.right != null) {
                        queue.offer(poll.right);
                    }
                }
                result.add(line);
            }
            return result;
        }

        // 个人实现
//    public List<List<Integer>> levelOrder(TreeNode root) {
//        if(root == null)
//            return new ArrayList<>();
//        List<List<Integer>> result = new ArrayList<>();
//        Queue<TreeNode> queue = new LinkedList<>();
//        queue.add(root);
//        TreeNode first = root;
//        List<Integer> line = new ArrayList<>();
//        while (!queue.isEmpty()) {
//            TreeNode poll = queue.poll();
//            if (poll == first) {
//                result.add(line);
//                line = new ArrayList<>();
//                first = null;
//            }
//            line.add(poll.val);
//            if (poll.left != null) {
//                queue.offer(poll.left);
//                if (first == null) {
//                    first = poll.left;
//                }
//            }
//            if (poll.right != null) {
//                queue.offer(poll.right);
//                if (first == null) {
//                    first = poll.right;
//                }
//            }
//        }
//        result.add(line);
//        return result.subList(1, result.size());
//    }
    }
//leetcode submit region end(Prohibit modification and deletion)

}